package bma.common.langutil.ai.stack;

import bma.common.langutil.ai.callback.AICallback;
import bma.common.langutil.ai.callback.AICallbackList;
import bma.common.langutil.ai.future.AIFuture;

/**
 * 
 * @author guanzhong
 * @since 1.0 (imported from BMA.ESP Project)
 * 
 * @param <RESP_TYPE>
 */
public class AIStackWrapForFuture<RESP_TYPE> extends AIStackWrap<RESP_TYPE>
		implements AIFuture<RESP_TYPE> {

	private AICallbackList<RESP_TYPE> callbackList;

	public AIStackWrapForFuture(AIStack<RESP_TYPE> delegate) {
		this(delegate, new AICallbackList<RESP_TYPE>());
	}

	protected AIStackWrapForFuture(AIStack<RESP_TYPE> delegate,
			AICallbackList<RESP_TYPE> clist) {
		super(delegate);
		callbackList = clist;
	}

	@Override
	public boolean success(RESP_TYPE result) {
		super.success(result);
		callbackList.execute(result, null);
		return true;
	}

	@Override
	public boolean failure(Throwable t) {
		super.failure(t);
		callbackList.execute(null, t);
		return true;
	}

	@Override
	public boolean callback(AICallback<RESP_TYPE> callback) {
		return addListener(callback);
	}

	@Override
	public boolean addListener(AICallback<RESP_TYPE> listener) {
		return this.callbackList.add(listener);
	}

}
